//
//  TestSendViewController.m
//  FDSMeshLibDemo
//
//  Created by linkiing on 2025/4/29.
//

#import "TestSendViewController.h"

@interface TestSendViewController ()

@property (nonatomic, assign) BOOL isStart;
@property (nonatomic, assign) NSInteger sendIndex;

@property (weak, nonatomic) IBOutlet UILabel *stateLabel;
@property (weak, nonatomic) IBOutlet UIView *showBgView;
@property (weak, nonatomic) IBOutlet UILabel *showLabel;
@property (weak, nonatomic) IBOutlet UISlider *showSlider;
@property (weak, nonatomic) IBOutlet UIButton *startButton;
@property (weak, nonatomic) IBOutlet UIButton *stopButton;

@end

@implementation TestSendViewController

#pragma mark - Initialize
- (void)InitializeCode{
    self.title = @"发包间隔测试";
    
    self.isStart = NO;
    self.sendIndex = 0;
    self.showBgView.layer.cornerRadius = 12;
    self.showBgView.layer.masksToBounds = YES;
    self.startButton.layer.cornerRadius = 22;
    self.startButton.layer.masksToBounds = YES;
    self.stopButton.layer.cornerRadius = 22;
    self.stopButton.layer.masksToBounds = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self InitializeCode];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.isStart = NO;
}

#pragma mark - UIButton
- (IBAction)clickStartBtn:(UIButton *)sender {
    if ([FDSMeshApi getConnectedNode]) {
        self.sendIndex = 0;
        [self startSendRun];
        [SVProgressHUD showInfoWithStatus:@"开始测试"];
    } else {
        [SVProgressHUD showErrorWithStatus:@"直连设备未连接"];
    }
}
- (IBAction)clickStopBtn:(UIButton *)sender {
    self.isStart = NO;
    self.showSlider.enabled = YES;
    [SVProgressHUD showInfoWithStatus:@"结束测试"];
}
- (void)startSendRun{
    NSTimeInterval timeValue = (self.showSlider.value*140.0 + 100.0) * 0.001;
    if (!self.isStart) {
        self.isStart = YES;
        self.showSlider.enabled = NO;
        self.stateLabel.hidden = NO;
        self.stateLabel.text = @"测试中...";
        NSOperationQueue *queue = [[NSOperationQueue alloc] init];
        [queue setMaxConcurrentOperationCount:1];
        [queue addOperationWithBlock:^{
            while (1) {
                UInt16 address = 0xFFFF;
                if ([self.testModel isKindOfClass:[FDSMeshNodeModel class]]) {
                    FDSMeshNodeModel *model = self.testModel;
                    address = model.address;
                } else {
                    FDSMeshGroupModel *model = self.testModel;
                    address = model.address;
                }
                BOOL isSwitch = self.sendIndex % 2 == 0 ? YES : NO;
                [FDSCommandApi changeLightSwitch:address isSwitch:isSwitch];
                NSLog(@"---SendRun---%ld, %@", (long)self.sendIndex, isSwitch ? @"YES" : @"NO");
                self.sendIndex ++;
                dispatch_async(dispatch_get_main_queue(), ^{
                    self.stateLabel.text = [NSString stringWithFormat:@"测试次数：%ld", (long)self.sendIndex];
                });
                if (!self.isStart) {
                    break;
                }
                [NSThread sleepForTimeInterval:timeValue];
            }
        }];
    }
}
- (IBAction)clickSliderChange:(UISlider *)sender {
    self.showLabel.text = [NSString stringWithFormat:@"%.0fms",(sender.value*140.0 + 100.0)];
}

@end
